package com.example.myapplication.old;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.myapplication.activity.BottomActivity;
import com.example.myapplication.R;

public class DemoActivity extends AppCompatActivity {
    private TextView view ;
    private EditText username ;
    private EditText password ;
    private View container;
    ImageView image;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_demo);
        view = findViewById(R.id.btn_login);
        username = findViewById(R.id.edtusername);
        password = findViewById(R.id.edtpwd);
        view.setEnabled(false);
        username.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if(!TextUtils.isEmpty(username.getText().toString())){
                    view.setEnabled(true);
                }
                else{
                    view.setEnabled(false);
                }
            }
        });

        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳转到另一个Activity
                Intent intent = new Intent(DemoActivity.this, BottomActivity.class);
                if(TextUtils.isEmpty(username.getText().toString())||TextUtils.isEmpty(password.getText().toString()))
                    Toast.makeText(getApplicationContext(), "用户名或者密码不能为空", Toast.LENGTH_SHORT).show();
                //Activity 跳转传递数据
                else {
                    //User user = new User(username.getText().toString(),password.getText().toString());
                    //intent.putExtra("user",user);
                    //intent.putExtra("username", username.getText().toString());
                    //intent.putExtra("password", password.getText().toString());
                    startActivity(intent);
                   // startActivityForResult(intent,1);
                }

            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode == RESULT_OK) {
            if (requestCode == 1) {
                username = findViewById(R.id.edtusername);
                Toast.makeText(getApplicationContext(), "设置成功", Toast.LENGTH_SHORT).show();
                String result = data.getStringExtra("result");
                username.setText(result);
            }
        }
    }
}